<script type="text/javascript">
 
    xx = function(){
        alert("xx");
    }

   AddForm={
        disable: function()
        {
            document.getElementById("documentName").setAttribute("disabled", "true");      
        }
    };

    var assoCount = 1;

    var assoString = "<table>\n\
        <tr> <td>Path <span class='required'>*</span></td>\n\
            <td>\n\
                <input type='text' name='aPathX' id='aPathX' value='http://a' />\n\
                <input type='button' class='button' value='..' title='Resource Tree' onclick='showResourceTree(\"aPathX\");'>\n\
        </td>\n\
        </tr>\n\
      </table>\n\
    <HR>";

   addAssoTable = function(){
      var dest = document.getElementById('diaAddAsso');
      var el = document.createElement('div');
      el.setAttribute('id', 'assoTable'+ assoCount);
      dest.appendChild(el);

      var assoEl = document.getElementById('assoTable' + assoCount );
      var tempAssoString = assoString.replace("aPathX", "aPath"+assoCount, "gm");

      assoEl.innerHTML = tempAssoString; 
        var typeForm = document.forms['diaAddForm'];
    //  alert(typeForm.numOfAsso.value);
      typeForm.numOfAsso.value = assoCount;

      assoCount++;
    };
    
    api = {
        test: function()
        {
          alert("test")  ;
        },

        loadPage: function(pageName, parameters){
            var path='';
            if (typeof parameters != 'undefined' ){
                var temp = parameters.substring(parameters.indexOf("path=")+5);
                path = temp.substring(0,temp.indexOf(','));
            }
            
            var pageURL= 'docs_ajaxprocessor.jsp';
            if(pageName == 'home'){
                pageURL= 'docs_ajaxprocessor.jsp';
            } else if (pageName == 'add'){
                pageURL = 'dia_add_ajaxprocessor.jsp';
            } else if (pageName == 'view'){
                if(path != ''){
                    pageURL = 'dia_view_ajaxprocessor.jsp?path='+path;
                } else{
                    pageURL = 'dia_view_ajaxprocessor.jsp';
                }
            }

            new Ajax.Request(pageURL,
              {
                method:'get',
                onSuccess: function(transport){
                  var response = transport.responseText || "no response text";
                  document.getElementById("content").innerHTML = response;
                },
                onFailure: function(){ alert('Something went wrong...') }
              });
        },

        setSPath: function(jsParentPath){
            
            var jsSPath =document.getElementsByName("documentName")[0].value;
            var docPath = jsParentPath+"/"+jsSPath;
            if(jsParentPath == "/" ){
                docPath = jsParentPath + jsSPath;
            }
            document.getElementById("sPath").setAttribute("value", docPath ); 
        },
        addAssociationTest: function(mainType){
            
            var typeForm = document.forms[mainType];
       //     alert(typeForm.aType.value);
       //     alert(typeForm.aPath.value);

          //  var parentPath = document.getElmentsByName("parentPath")[0].value;
          //  var documentName = document.getElementsByName("documentName")[0].value;
          //  alert(parentPath+"../"+documentName);
          alert("sssss "+document.getElementsByName("sPath")[0].value);
           var sourcePath = document.getElementsByName("sPath")[0].value;
           
            var aTypex = "related-document";//typeForm.aType.value;
            var aPathx = typeForm.aPath.value;

            new Ajax.Request('../relations/association-list-ajaxprocessor.jsp',
                {

                method:'post',
                parameters:{path:sourcePath,type:aTypex, associationPaths:aPathx, todo:'add'},
                onSuccess: function(transport) {
                    var response = transport.responseText;
                    var re = /associationsDiv/gi;
                    response = response.replace(re, "diaAssoDiv");
                 //   response = response.replace("associationsDiv", 'diaAssoDiv', "g");
 
                   $('diaAssoDiv').innerHTML = response;
                   // alert(transport.responseText);
                    alert("successss "+response);

                },
                onFailure: function(transport) {
                    CARBON.showErrorDialog(transport.responseText);
                    alert("fail");
                    
                    }
                }

/* new Ajax.Request('../relations/association-list-ajaxprocessor.jsp',
                {

                    method:'post',
                    parameters:{path:"/",type:"diaAssoType",associationPaths:"http://diaAssociationPaths.com",todo:'add'},
                    onSuccess: function(transport) {
                        alert("saasa");
                        $(fillingDiv).innerHTML = transport.responseText;
                        $(fillingDiv).appendChild(tempSpan);
                        $(fillingDiv).style.display = "";
                        if (fillingDiv == "diaAssociationsDiv") {
                            $('associationsIconExpanded').style.display = "";
                            $('associationsIconMinimized').style.display = "none";

                            YAHOO.util.Event.onAvailable('updateFix', function() {
                                $('diaAssociationsSum').style.display = "none";
                                $('diaAssociationsList').style.display = "";

                            });


                        } else {
                            alert("faile");
                            $('dependenciesIconExpanded').style.display = "";
                            $('dependenciesIconMinimized').style.display = "none";
                            YAHOO.util.Event.onAvailable('updateFix', function() {
                                $('dependenciesSum').style.display = "none";
                                $('dependenciesList').style.display = "";
                            });

                        }alert("after");

                    },
                    onFailure: function(transport) {
                        CARBON.showErrorDialog(transport.responseText);
                    }
                }*/


);

        },

        addAssociation: function(mainType)
        {
            
            var typeForm = document.forms[mainType];
//            var addDivId = 'diaAssociationsAddDiv';
//            var fillingDiv = 'diaAssociationsDiv';
//            var diaAssoType = 'depends';
//            var reason = "";
//            if ($('updateFix')) {
//                $('updateFix').parentNode.removeChild($('updateFix'));
//            }
//            var tempSpan = document.createElement('span');
//            tempSpan.id = "updateFix";
//
//            if (mainType != "depForm") {
//                reason += validateForInput(typeForm.diaType, org_wso2_carbon_registry_relations_ui_jsi18n["type"]);
//                if (reason == "") {
//               //     reason += validateEmpty(typeForm.diaType, org_wso2_carbon_registry_relations_ui_jsi18n["type"]);
//                }
//                if (reason == "") {
//                    reason += validateNotExists(typeForm.diaAssociationPaths.value);
//                }
//
//                if (reason == "") {
//                    typeForm.diaType.value = typeForm.diaType.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
//                    if (typeForm.diaType.value == "") {
//               //         reason = org_wso2_carbon_registry_relations_ui_jsi18n["association.type.cannot.contain.only.white.spaces"];
//                    }
//                }
                diaAssoType = typeForm.diaType.value;
                alert(diaAssoType);
//                if (diaAssoType == "depends") {
//                    fillingDiv = 'dependenciesDiv';
//                }
//            } else {
//
//                fillingDiv = 'dependenciesDiv';
//                addDivId = "dependenciesAddDiv";
//            }
//
//            if (reason == "") {
//           //     reason += validateForInput(typeForm.diaAssociationPaths, org_wso2_carbon_registry_relations_ui_jsi18n["path"]);
//            }
//            if (reason == "") {
          //      reason += validateEmpty(typeForm.diaAssociationPaths, org_wso2_carbon_registry_relations_ui_jsi18n["path"]);
//            }
//            if (reason == "") {
//                reason += validateNotExists(typeForm.diaAssociationPaths.value);
//            }
//            if (reason == "") {
//                reason += validateAssocationNotExists($('diaResourcePath').value,
//                                    typeForm.diaAssociationPaths.value, diaAssoType);
//            }
//
//            if (reason == "") {
//                typeForm.diaAssociationPaths.value = typeForm.diaAssociationPaths.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
//                if (typeForm.diaAssociationPaths.value == "") {
//                  reason = org_wso2_carbon_registry_relations_ui_jsi18n["path.cannot.contain.only.white.spaces"];
//                }
//            }

          // if (reason == "") {
           if (true) {
                var diaResourcePath = $('diaResourcePath').value;
                alert(diaResourcePath);
                var diaAssociationPaths = "http://dsd.ds";
                    //typeForm.diaAssociationPaths.value;
  //              typeForm.diaType.value = "";
    //            typeForm.diaAssociationPaths.value = "";
//                showHideCommon(addDivId);
//                alert("saasa");
//"diaResourcePath"
        alert("add asso func");
                new Ajax.Request('../relations/association-list-ajaxprocessor.jsp',
                {
                    
                    method:'post',
                    parameters:{path:"/",type:diaAssoType,associationPaths:diaAssociationPaths,todo:'add'},
                    onSuccess: function(transport) {
                        alert("success");
                        $(fillingDiv).innerHTML = transport.responseText;
                        $(fillingDiv).appendChild(tempSpan);
                        $(fillingDiv).style.display = "";
                        if (fillingDiv == "diaAssociationsDiv") {
                            $('associationsIconExpanded').style.display = "";
                            $('associationsIconMinimized').style.display = "none";

                            YAHOO.util.Event.onAvailable('updateFix', function() {
                                $('diaAssociationsSum').style.display = "none";
                                $('diaAssociationsList').style.display = "";
                            });
 
                        } else {
                            alert("faile");
                            $('dependenciesIconExpanded').style.display = "";
                            $('dependenciesIconMinimized').style.display = "none";
                            YAHOO.util.Event.onAvailable('updateFix', function() {
                                $('dependenciesSum').style.display = "none";
                                $('dependenciesList').style.display = "";
                            });

                        }

                    },
                    onFailure: function(transport) {
                        alert("failed");
                        CARBON.showErrorDialog(transport.responseText);
                    }
                }

/* new Ajax.Request('../relations/association-list-ajaxprocessor.jsp',
                {

                    method:'post',
                    parameters:{path:"/",type:"diaAssoType",associationPaths:"http://diaAssociationPaths.com",todo:'add'},
                    onSuccess: function(transport) {
                        alert("saasa");
                        $(fillingDiv).innerHTML = transport.responseText;
                        $(fillingDiv).appendChild(tempSpan);
                        $(fillingDiv).style.display = "";
                        if (fillingDiv == "diaAssociationsDiv") {
                            $('associationsIconExpanded').style.display = "";
                            $('associationsIconMinimized').style.display = "none";

                            YAHOO.util.Event.onAvailable('updateFix', function() {
                                $('diaAssociationsSum').style.display = "none";
                                $('diaAssociationsList').style.display = "";

                            });


                        } else {
                            alert("faile");
                            $('dependenciesIconExpanded').style.display = "";
                            $('dependenciesIconMinimized').style.display = "none";
                            YAHOO.util.Event.onAvailable('updateFix', function() {
                                $('dependenciesSum').style.display = "none";
                                $('dependenciesList').style.display = "";
                            });

                        }alert("after");

                    },
                    onFailure: function(transport) {
                        CARBON.showErrorDialog(transport.responseText);
                    }
                }*/


);

            }
            else {
                CARBON.showWarningDialog(reason);
            }
    },

    applyTag: function(e){
        var reason="";

        var keyCode;
        if(e!='null'){
                if (window.event) {
                    keyCode = window.event.keyCode;
                } else {
                    keyCode = e.which;
                }
        }

        if (keyCode == 13 || keyCode == 0 || keyCode == 1) {

            var path = document.getElementById('sPath').value;
            var tag = $('diaTags').value;
            reason+=validateForInput($('diaTags'),org_wso2_carbon_registry_info_ui_jsi18n["tag.name"]);
            reason+=validateEmpty($('diaTags'),org_wso2_carbon_registry_info_ui_jsi18n["tag.name"]);

            //reason+=validateIllegal($('tfTag'),"Tag Name");
            if(reason==""){
                new Ajax.Request('../info/tag-ajaxprocessor.jsp', {
                    method: 'post',
                    parameters: {tag: tag, path: path},

                    onSuccess: function(transport) {
                        $('diaTagDiv').innerHTML = transport.responseText;
                    },

                    onFailure: function(transport) {
                        showRegistryError(transport.responseText);
                    }
                });
                $('diaTags').value = "";
                showHideCommon('diaTagAddTable');
            }
            else{
                CARBON.showWarningDialog(reason);
            }
        }
    }

};
</script>
